﻿// Operations and events related to setting or getting a value from an element (form field)
define([
    'Wjs/Wjs'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'Valuable',

        internal: {

            listeners: {

                onDomElementCreated: function () {

                    if (this.value) {

                        this.setValue(this.value); // setValue might be ovverriden by other elements

                        delete this.value;
                    }
                }
            }
        },

        onValueSet: function(fcn) {

            this.valueSetHandler = Wjs.chain(this.valueSetHandler, fcn);
        },

        setValue: function (val, options) {

            if (this.isRendered()) {

                this.domEl.value = val;
            }
            else {

                this.value = val;
            }

            if (this.valueSetHandler) {

                this.valueSetHandler(options);
            }
            
            return this;
        },

        getValue: function () {
            var val;

            if (this.isRendered()) {

                val = this.domEl.value;
            }
            else {
                val = this.value;
            }

            //this.dataType.convert.call(this, val);

            return val;
        }

    });

});